package com.nuc.kl.module.usermanager.action;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.basedata.entity.Navigation;
import com.nuc.kl.module.usermanager.entity.Privilege;
import com.nuc.kl.module.usermanager.entity.Role;
import com.nuc.kl.module.usermanager.entity.Teacher;
import com.nuc.kl.module.usermanager.entity.TreeDTO;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class PrivilegeAction extends BaseAction<Privilege>{
	
	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用

	private JSONObject result;// 返回的json
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	private Long parentId; 
	/** 列表 */
	public String listJson() throws Exception {
		List<Privilege> list = privilegeService.findList(model.getId());
		
		StringBuffer json = new StringBuffer("[");
		for (Privilege pri : list) {
			
			String parentId = pri.getParent() == null ?null:(pri.getParent().getId()+"").trim();
			String url = pri.getUrl() == null ?"": pri.getUrl();
			
			String state = pri.getState() == null ?"open":pri.getState();
			
			json.append("{\"id\":" + pri.getId() 
						+ ",\"name\":\"" + pri.getName() + "\"" 
						+ ",\"url\":\"" + url + "\"" 
						+ ",\"_parentId\":\"" + parentId + "\""
						+ ",\"state\":\"" + state+ "\"},")
						;
		}
		
		String jsonString = json.toString().substring(0, json.length()-1) + "]";
		System.out.println(jsonString);
		
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(jsonString);		
		return null;
	}
	
	/** 列表 */
	public String list() throws Exception {
		
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		privilegeService.delete(model.getId());
		
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		
		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		model.setParent(privilegeService.getById(parentId));
		privilegeService.save(model);
		
		Privilege privilege = privilegeService.getById(parentId);
		privilege.setState("closed");
		privilegeService.update(privilege);
		
		privilegeService.save(model);
		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		Privilege privilege = privilegeService.getById(model.getId());
		ActionContext.getContext().put("privilege",privilege);
		
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		Privilege privilege = privilegeService.getById(model.getId());
		privilege.setName(model.getName());
		privilege.setUrl(model.getUrl());
		privilegeService.update(privilege);
		return "toList";
	}
	
	/** 同时删除多个 */
	public String deleteByIds() throws Exception {
		String[] idList = ids.split(",");
		for (String str : idList) {
			roleService.delete(Long.parseLong(str));
		}
		return "toList";
	}
	
	/** 设置权限页面 */
	public String getTree() throws Exception {
		String id = null;
		if(model.getId()!=null)
		{
			id=model.getId().toString();
		}
		List<TreeDTO> treeList = privilegeService.getChildrenByParentId(id);
		response.getWriter().write(JSONArray.fromObject(treeList).toString());
		
			
		return null;
	}
	
	public String index() throws Exception {
		
		return "index";
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	
	
}
